extends CenterContainer

var data = null

var music_area_id = null

onready var fight_box = $PanelContainer/VBoxContainer/fightBox

const NpcFightItem = preload("res://tool/page/map/mapView/npc/NpcFightItem.tscn")


func _ready():
	pass 



func set_data(data):
	self.data = data
	
	$PanelContainer/VBoxContainer/Label.text = "【%s】%s" % [data["id"], data.get("name", "")]
	
	var npc_fight_list = DataSource.get_data_by_list(DataSource.npc_fight, data["id"], "npcId")
	
	for child in fight_box.get_children():
		child.queue_free()
	
	for npc_fight in npc_fight_list:
		var ui = NpcFightItem.instance()
		fight_box.add_child(ui)
		
		ui.set_data(npc_fight)
		
		ui.connect("edit", self, "_on_fight_edit")
		ui.connect("delete", self, "_on_delete")
	
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	$PanelContainer/VBoxContainer/select.visible = map_view.select_entity_mode
	
	
func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_fight_edit(data):
	MainTool.getUILayer().pop_ui()
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/npc/modal/NpcFightModal.tscn", 0, false)
	ui.set_edit_data(self.data, data)



func _on_delete(data):
	MainTool.confirm("你是否确认删除Npc战斗: %s" % data["name"], self, "_on_fight_del_ok", null, data)
	pass

func _on_fight_del_ok(data):
	MainTool.getUILayer().pop_ui()
	DataSource.delete_data_by_id(DataSource.npc_fight, data["id"])
	
	MainTool.toast_info("删除成功")

# 点击删除
func _on_delete_button_up():
	MainTool.confirm("你是否确认删除Npc: %s" % data.get("name", ""), self, "_on_del_ok", null, data)

func _on_del_ok(data):
	MainTool.getUILayer().pop_ui()
	
	DataSource.delete_data_by_id(DataSource.map_npc, data["id"])
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	map_view.entity_update()


# 点击基本信息
func _on_info_button_up():
	MainTool.getUILayer().pop_ui()
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/modal/MapNpcModal.tscn", 0, false)
	
	ui.set_edit_data(data)

# 点击添加战斗
func _on_fight_button_up():
	MainTool.getUILayer().pop_ui()
	
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/npc/modal/NpcFightModal.tscn", 0, false)
	ui.set_edit_data(data)
	


func _on_fightSprite_button_up():
	var npc_fight = DataSource.get_data_by_one(DataSource.npc_fight, data["id"], "npcId")
	
	if npc_fight:
		MainTool.getUILayer().pop_ui()
		var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/npc/modal/NpcFightSpriteModal.tscn", 0, false)
		
		ui.set_npc_data(data)
	else:
		MainTool.toast_info("请配置好战斗信息后在配置队伍")
	


func _on_select_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	map_view.emit_select_signal(1, data)
	
	MainTool.getUILayer().pop_ui()
	MainTool.getUILayer().pop_ui()
